package com.util.fileOperation;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述
 *
 * @author zhu_jinsheng
 * @version 1.0
 * @date 2020/11/10 10:27
 */
@Slf4j
public class EasyExcelUtil {

    /**
     * 入参 文件流 和 第size行数据为表头不解析 返回List<List<String>>
     *
     * @param inputStream
     * @param size
     * @return
     */
    public static List<List<String>> readExcel(InputStream inputStream, int size) {
        List<List<String>> list = new ArrayList<>();
        try {
            List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0));
            int i = 0;
            for (Object object : data) {
                @SuppressWarnings("unchecked")
                List<String> object1 = (List<String>) object;
                // 每列的第一列数据不能为空，前两行为表头
                if (null == object1 || StringUtils.isEmpty(object1.get(0)) || i < size) {
                    i = i + 1;
                } else {
                    list.add(object1);
                }
            }
        } catch (Exception e) {
            log.error("解析excel失败", e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("解析批量进件excel失败", e);
            }
        }
        return list;
    }

    /**
     * 导出示例
     * @param outputStream
     */
    public static void writeExcel(OutputStream outputStream) {
        ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX, true);
        Sheet sheet = new Sheet(0);
        // 设置表头
        String[] head = {"标题1", "标题2", "标题3", "标题4"};
        sheet.setHead(getHead(head));
        // 设置表名
        sheet.setSheetName("表1");
        Table table = new Table(1);
        List<List<String>> data1 = new ArrayList<>();
        String[] content1 = {"内容1", "内容2", "内容3", "内容4"};
        String[] content2 = {"内容5", "内容6", "内容7", "内容8"};
        List<String> list1 = Arrays.asList(content1);
        List<String> list2 = Arrays.asList(content2);
        data1.add(list1);
        data1.add(list2);
        List<List<String>> data2 = new ArrayList<>();
        String[] content3 = {"测试1", "测试2", "测试3", "测试4"};
        String[] content4 = {"测试5", "测试6", "测试7", "测试8"};
        List<String> list3 = Arrays.asList(content3);
        List<String> list4 = Arrays.asList(content4);
        data2.add(list3);
        data2.add(list4);
        writer.write0(data1, sheet, table);
        writer.write0(data2, sheet, table);
        writer.write0(data1, sheet, table);
        writer.finish();
    }

    public static List<List<String>> getHead(String[] head) {
        List<List<String>> lists = new ArrayList<>();
        for (String s : head) {
            List<String> list = new ArrayList<>();
            list.add(s);
            lists.add(list);
        }
        return lists;
    }

    /**
     * 设置导出文件名
     * @param response 响应体
     * @param fileName 文件名
     * @param excelType XLS 和 XLSX
     * @throws Exception
     */
    public static void setExportFileName(HttpServletResponse response,
                                   String fileName,
                                   ExcelTypeEnum excelType) throws Exception {
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition",
                "attachment;filename=" + new String(fileName.getBytes("utf-8"),
                        "iso8859-1") + excelType.getValue());
    }
}
